package com.example.strategy.context;

import com.example.strategy.strategy.PaymentStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description : 策略模式
 * @Author : Bruce Lee
 * @CreateTime : 2025/8/14
 */

@Component
public class PaymentContext {

    /**
     * 自动将PaymentStrategy的实现类对象注入到strategyHashMap中，key为实现类名(可自定义)，value为实现类对象
     * # 不知为何错误 private HashMap<String, PaymentStrategy> strategyHashMap = new HashMap<>();
     */

    @Autowired
    private Map<String, PaymentStrategy> strategyHashMap = new ConcurrentHashMap<>();

    public void pay(String type, int amount) {
        PaymentStrategy paymentStrategy = strategyHashMap.get(type);
        paymentStrategy.pay(amount);
    }

}
